package com.origin.file.entity.domain;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.origin.common.util.DateUtil;
import com.origin.file.io.QiwenFile;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

/**
 * 用户文件
 *
 * @author origin
 */
@TableName("user_file")
@Data
@NoArgsConstructor
public class UserFile {

    @TableId(value = "user_file_id", type = IdType.INPUT)
    private String userFileId;

    /**
     * 用户 id
     */
    @TableField("user_id")
    private String userId;

    /**
     * 文件 id
     */
    @TableField("file_id")
    private String fileId;

    /**
     * 文件名
     */
    @TableField("file_name")
    private String fileName;

    /**
     * 扩展名
     */
    @TableField("extend_name")
    private String extendName;

    /**
     * 文件路径
     */
    @TableField("file_path")
    private String filePath;

    /**
     * 是否是目录
     */
    @TableField("is_dir")
    private Integer isDir;

    /**
     * 上传时间
     */
    @TableField("upload_datetime")
    private LocalDateTime uploadDateTime;

    /**
     * 删除批次号
     */
    @TableField("delete_batch_num")
    private String deleteBatchNum;

    /**
     * 创建用户 id
     */
    @TableField("create_user_id")
    private String createUserId;

    /**
     * 修改用户 id
     */
    @TableField("modify_user_id")
    private String modifyUserId;

    /**
     * 创建时间
     */
    @TableField("create_datetime")
    private LocalDateTime createDateTime;

    /**
     * 修改时间
     */
    @TableField("modify_datetime")
    private LocalDateTime modifyDateTime;

    /**
     * 删除时间
     */
    @TableField("delete_datetime")
    private LocalDateTime deleteDateTime;

    /**
     * 删除标识
     */
    @TableField("delete_flag")
    private Integer deleteFlag;

    public UserFile(QiwenFile qiwenFile, String userId, String fileId) {
        this.userFileId = IdUtil.getSnowflakeNextIdStr();
        this.userId = userId;
        this.fileId = fileId;
        this.filePath = qiwenFile.getParent();
        this.fileName = qiwenFile.getNameNotExtend();
        this.extendName = qiwenFile.getExtendName();
        this.isDir = qiwenFile.isDirectory() ? 1 : 0;
        LocalDateTime now = LocalDateTime.now();
        this.setUploadDateTime(now);
        this.setCreateUserId(userId);
        this.setCreateDateTime(now);
        this.deleteFlag = 0;
    }

    public boolean isDirectory() {
        return this.isDir == 1;
    }

    public boolean isFile() {
        return this.isDir == 0;
    }

}
